//
//  MessageViewController.m
//  ZJControlsClass
//
//  Created by apple on 2024/10/22.
//

#import "MessageViewController.h"
#import "MessageAllNoteViewController.h"
//#import "MessageSystomNoteViewController.h"
//#import "MessageApproveNoteViewController.h"
#import "ZJMessageMainNetworking.h"
#import "PendChildCollectionViewCell.h"



@interface MessageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

//@property (nonatomic,strong) UIView * searchView;
//@property (nonatomic,strong) UIImageView * img;
//@property (nonatomic,strong) UITextField * searchTextF;
//@property (nonatomic,strong) UIButton * allBtn;

@property (nonatomic,strong) UILabel * titileLabel;


@property (nonatomic,strong) UICollectionView * tagCollectionView;
@property (nonatomic,strong) NSMutableArray * tagArr;
@property (nonatomic,strong) NSMutableArray * tagSelectArr;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSArray * childDataSource;
//@property (nonatomic,strong) MessageAllNoteViewController *oneVC;
//@property (nonatomic,strong) MessageSystomNoteViewController *twoVC;
//@property (nonatomic,strong) MessageApproveNoteViewController *thereVC;
@property (nonatomic,strong) ZJMessageMainNetworking * unReatManager;
@end

@implementation MessageViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:true];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.navigationController setNavigationBarHidden:false];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpUI];
    
    self.tagArr = [NSMutableArray arrayWithObjects:@"全部",@"系统消息",@"审批消息",@"违规消息", nil];
    self.tagSelectArr = [NSMutableArray arrayWithObjects:@"1",@"0",@"0",@"0", nil];
    
    [self networking];
//    [[ZJBaiDuFaceManager shared] initFaceServiceAndInfoCollectService:self];
//    [self initFaceServiceAndInfoCollectService];
}

-(void)networking{
    
    
    WeakSelf(self)
//    __weak typeof(self) weakSelf = self;
//    [self.undoManager di]
    [self.unReatManager dispatchTaskWithParam:@{} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
                
//            if (weakSelf.pageNo == 1) {
//                [weakSelf.dataSource removeAllObjects];
//            }
//            NSArray * data = responseObject[@"data"][@"list"];
//            if (data.count < 20)[self.tableView.mj_footer endRefreshingWithNoMoreData];
//            else [weakSelf.tableView.mj_footer endRefreshing];
//            [weakSelf.dataSource  addObjectsFromArray:[ZJMessageListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
//               
//            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
        
//        [self.tableView.mj_header endRefreshing];
        
        
    }];
    
    
}

-(void)setUpUI{
    
//    [self.view addSubview:self.searchView];
//    [self.searchView addSubview:self.img];
    
    [self.view addSubview:self.titileLabel];
   
    [self.titileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset([[UIApplication sharedApplication] statusBarFrame].size.height);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(44));
    }];
    
//    [self.searchView addSubview:self.searchTextF];
    
    
    [self.view addSubview:self.tagCollectionView];
    [self.view addSubview:self.collectionView];
    
//    [self.view addSubview:self.allBtn];
    
//    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(12));
//        make.top.equalTo(self.view).with.offset(StatusBarHeight - 46);
//        make.height.equalTo(@(ALIGN_HEIGHT_PT(35)));
//        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(87));
//    }];
//    
//    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.searchView.mas_centerY);
//        make.left.equalTo(self.searchView).with.offset(ALIGN_WIDTH_PT(15));
//        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(17), ALIGN_WIDTH_PT(17))));
//    }];
//    
//    [self.searchTextF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.right.bottom.equalTo(self.searchView);
//        make.left.equalTo(self.img.mas_right);
//    }];
//    
//    [self.allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.searchView.mas_right);
//        make.top.equalTo(self.searchView.mas_top);
//        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(12));
//        make.height.equalTo(@(ALIGN_HEIGHT_PT(35)));
//    }];
//    
    
    [self.tagCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(StatusBarHeight + ALIGN_HEIGHT_PT(5));
//        make.top.equalTo(self.searchView.mas_bottom).with.offset(ALIGN_HEIGHT_PT(5));
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
    }];
        
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.tagCollectionView.mas_bottom);
    }];
    
}


-(void)clickCleanAllMeesage {

//    [[ZJBaiDuFaceManager shared] startCheckWithSuccessBlock:^{
//
//    } failureBlock:^(NSString * _Nonnull errorMsg) {
//
//    }];
//    [self startCheck];
}


#pragma mark -- protocolClickButtonChangeCollectionViewContentOffsetDelegate
-(void)ClickButtonWithIndex:(NSInteger)index{
    CGFloat offsetX = index * SCREEN_WIDTH;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0)];
    
    
    [self.tagSelectArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.tagSelectArr replaceObjectAtIndex:idx withObject:@"0"];
    }];
    [self.tagSelectArr replaceObjectAtIndex:index withObject:@"1"];
    [self.tagCollectionView reloadData];
    
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.tagCollectionView]) {
        return self.tagArr.count;
    }
    return self.childDataSource.count;
}


-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([collectionView isEqual:self.tagCollectionView]) {
        PendChildCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PendChildCollectionViewCell" forIndexPath:indexPath];
        
        if ([self.tagSelectArr[indexPath.row] isEqualToString:@"1"]) {
            cell.follewView.backgroundColor = COLOR_RGB(50, 113, 237);
            cell.label.textColor = COLOR_RGB(50, 113, 237);
        }else{
            cell.follewView.backgroundColor = [UIColor clearColor];
            cell.label.textColor = COLOR_RGB(102, 102, 102);
        }
        cell.label.text = [NSString stringWithFormat:@"%@",self.tagArr[indexPath.row]];
        
        return cell;
    }else{
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
        UIViewController *vc = self.childDataSource[indexPath.row];
            [cell.contentView addSubview:vc.view];
            [vc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.top.equalTo(cell.contentView);
            }];
        return cell;
        
    }
    return nil;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([collectionView isEqual:self.tagCollectionView]) {
        
        CGFloat offsetX = indexPath.row * SCREEN_WIDTH;
        [self.collectionView setContentOffset:CGPointMake(offsetX, 0)];
        
        
        [self.tagSelectArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tagSelectArr replaceObjectAtIndex:idx withObject:@"0"];
        }];
        [self.tagSelectArr replaceObjectAtIndex:indexPath.row withObject:@"1"];
        [self.tagCollectionView reloadData];
        
    }
}





#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual: self.tagCollectionView]) {
        return CGSizeMake(SCREEN_WIDTH / 4, ALIGN_HEIGHT_PT(45));
    }else{
        return self.collectionView.bounds.size;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}




#pragma mark -- 懒加载
//-(UIView *)searchView{
//    if (!_searchView){
//        _searchView = [UIView new];
//        _searchView.backgroundColor = COLOR_RGB(242, 245, 248);
//        _searchView.layer.cornerRadius = 35 / 2;
//        _searchView.layer.masksToBounds = YES;
//        _searchView.userInteractionEnabled = YES;
//    }
//    return _searchView;
//}
//
//-(UIImageView *)img{
//    if (!_img){
//        _img = [UIImageView new];
//        _img.image = [UIImage imageNamed:@"search"];
//    }
//    return _img;
//}
//
//-(UITextField *)searchTextF{
//    if (!_searchTextF){
//        _searchTextF = [UITextField new];
//        _searchTextF.placeholder = @"请输入关键字搜索";
//        _searchTextF.textColor = [UIColor blackColor];
//        _searchTextF.font = FONT14;
//        _searchTextF.textAlignment = NSTextAlignmentLeft;
//    }
//    return _searchTextF;
//}
//
//-(UIButton *)allBtn{
//    if (!_allBtn){
//        _allBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        [_allBtn setTitle:@"全部已读" forState:(UIControlStateNormal)];
//        [_allBtn setTitleColor:COLOR_RGB(102, 102, 102) forState:(UIControlStateNormal)];
//        [_allBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//        _allBtn.titleLabel.font = FONT14;
//        [_allBtn addTarget:self action:@selector(clickCleanAllMeesage) forControlEvents:UIControlEventTouchUpInside];
////        [_allBtn addTarget:self action:@selector(clickCleanAllMeesage) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _allBtn;
//}

-(UICollectionView *)tagCollectionView{
    if (!_tagCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _tagCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _tagCollectionView.delegate = self;
        _tagCollectionView.dataSource = self;
        _tagCollectionView.userInteractionEnabled = YES;
        _tagCollectionView.backgroundColor = [UIColor clearColor];
        _tagCollectionView.showsVerticalScrollIndicator = false;
        _tagCollectionView.showsHorizontalScrollIndicator = false;
        
        // cell
        [_tagCollectionView registerClass:[PendChildCollectionViewCell class] forCellWithReuseIdentifier:@"PendChildCollectionViewCell"];
    }
    return _tagCollectionView;
}



- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        _collectionView.backgroundColor = [UIColor clearColor];
    }
    return _collectionView;
}

- (NSArray *)childDataSource{
    if (!_childDataSource) {
        
        MessageAllNoteViewController * oneVC = [[MessageAllNoteViewController alloc] init];
        oneVC.type = @"";
        [self addChildViewController:oneVC];
        
        MessageAllNoteViewController * twoVC = [[MessageAllNoteViewController alloc] init];
        twoVC.type = @"1";
        [self addChildViewController:twoVC];
        
        
        MessageAllNoteViewController * thereVC = [[MessageAllNoteViewController alloc] init];
        thereVC.type = @"2";
        [self addChildViewController:thereVC];
        
        MessageAllNoteViewController * fourVC = [[MessageAllNoteViewController alloc] init];
        fourVC.type = @"3";
        [self addChildViewController:fourVC];
        
        
        _childDataSource = @[oneVC, twoVC,thereVC,fourVC];
        
    }
    return _childDataSource;
}

//-(MessageAllNoteViewController *)oneVC{
//    if (!_oneVC){
//        _oneVC = [MessageAllNoteViewController new];
//    }
//    return _oneVC;
//}
//
//-(MessageSystomNoteViewController *)twoVC{
//    if (!_twoVC){
//        _twoVC = [MessageSystomNoteViewController new];
//    }
//    return _twoVC;
//}
//
//-(MessageApproveNoteViewController *)thereVC{
//    if (!_thereVC){
//        _thereVC = [MessageApproveNoteViewController new];
//    }
//    return _thereVC;
//}

-(ZJMessageMainNetworking *)unReatManager{
    if (!_unReatManager){
        _unReatManager = [ZJMessageMainNetworking new];
    }
    return _unReatManager;
}


-(UILabel *)titileLabel{
    if (!_titileLabel) {
        _titileLabel = [UILabel new];
        _titileLabel.text = @"消息";
        _titileLabel.textColor = [UIColor blackColor];
        _titileLabel.textAlignment = NSTextAlignmentCenter;
        _titileLabel.font = FONT18;
    }
    return _titileLabel;
}

@end
